package com.tentative.competition.schedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tentative.competition.common.core.result.PageResult;
import com.tentative.competition.common.model.schedule.param.AttendInfoAddParam;
import com.tentative.competition.common.model.schedule.resp.AttendConfigInfoResp;
import com.tentative.competition.common.model.schedule.resp.AttendInfoResp;
import com.tentative.competition.common.model.schedule.vo.FileVo;
import com.tentative.competition.schedule.domain.ScheduleAttendInfo;
import com.tentative.competition.schedule.domain.ScheduleAward;
import com.tentative.competition.schedule.mapper.ScheduleAttendInfoMapper;
import com.tentative.competition.schedule.service.ScheduleAttendInfoService;
import com.tentative.competition.schedule.service.ScheduleFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 参赛信息 服务实现类
 * </p>
 *
 * @author SmileH
 * @since 2023-06-22
 */
@Service
public class ScheduleAttendInfoServiceImpl extends ServiceImpl<ScheduleAttendInfoMapper, ScheduleAttendInfo> implements IService<ScheduleAttendInfo>, ScheduleAttendInfoService {
    @Lazy
    @Autowired
    private ScheduleFileInfoService fileInfoService;
    @Override
    public PageResult<AttendInfoResp> pageList(AttendInfoAddParam param) {
        IPage<AttendInfoResp> awardResps = baseMapper.pageList(param,new Page<>(param.getPageNum(),param.getPageSize()));
        if (CollUtil.isEmpty(awardResps.getRecords())){
            return new PageResult<AttendInfoResp>();
        }
        //组装附件信息
        Set<Long> fileIds = awardResps.getRecords().stream().map(AttendInfoResp::getAttendImg).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(fileIds)){
            Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(fileIds);
            awardResps.getRecords().forEach(x->x.setAttendImgVo(longFileVoMap.get(x.getAttendImg())));
        }
        PageResult pageResult = new PageResult<>();
        pageResult.setContent(awardResps.getRecords());
        pageResult.setTotal(awardResps.getTotal());
        return pageResult;
    }

    @Override
    public AttendInfoResp getInfo(Long id) {
        AttendInfoResp resp = new AttendInfoResp();
        ScheduleAttendInfo attendInfo = this.lambdaQuery()
                .eq(ScheduleAttendInfo::getId, id)
                .list()
                .stream()
                .findFirst()
                .orElse(null);
        if (ObjUtil.isNotEmpty(attendInfo)){
            BeanUtil.copyProperties(attendInfo,resp);
            //组装附件
            if (ObjUtil.isNotEmpty(resp.getAttendImg())){
                Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(CollUtil.newHashSet(resp.getAttendImg()));
                resp.setAttendImgVo(longFileVoMap.get(resp.getAttendImg()));
            }
        }
        return resp;
    }

    @Override
    public void addOrEdit(AttendInfoAddParam param) {
        ScheduleAttendInfo info = new ScheduleAttendInfo();
        BeanUtil.copyProperties(param,info);
        this.saveOrUpdate(info);
    }

    @Override
    public Boolean deleteOrgInfo(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            throw new RuntimeException("没有需要删除的数据！");
        }
        return this.lambdaUpdate()
                .in(ScheduleAttendInfo::getId,ids)
                .remove();
    }

    @Override
    public List<AttendConfigInfoResp> getConfigInfo(Long scheduleId) {
        List<AttendConfigInfoResp> list = CollUtil.newArrayList();
        List<ScheduleAttendInfo> attendInfos = this.lambdaQuery()
                .eq(ScheduleAttendInfo::getScheduleId, scheduleId)
                .eq(ScheduleAttendInfo::getDelFlag, 0)
                .list();
        if (CollUtil.isEmpty(attendInfos)){
            return list;
        }
        List<AttendConfigInfoResp> resps = attendInfos.stream().map(x -> {
            AttendConfigInfoResp resp = new AttendConfigInfoResp();
            resp.setScheduleId(x.getScheduleId());
            resp.setAttendImg(x.getAttendImg());
            resp.setAttendTitle(x.getAttendTitle());
            resp.setAttendContent(x.getAttendContent());
            resp.setAttendPosition(x.getAttendPosition());
            return resp;
        }).collect(Collectors.toList());
        //组装附件
        Set<Long> fileIds = resps.stream().map(AttendConfigInfoResp::getAttendImg).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(fileIds)){
            Map<Long, FileVo> longFileVoMap = fileInfoService.selectFileMapByIds(fileIds);
            resps.forEach(x->x.setAttendImgUrl(longFileVoMap.containsKey(x.getAttendImg()) ? longFileVoMap.get(x.getAttendImg()).getFileUrl() : ""));
        }
        return resps;
    }
}
